package com.qingzhou.web.service.impl.convert;


import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.mybatisflex.core.query.QueryWrapper;
import com.qingzhou.common.web.domain.SysUser;
import com.qingzhou.system.domain.SysPost;
import com.qingzhou.system.service.ISysPostService;
import com.qingzhou.system.service.ISysUserService;
import org.noear.solon.Solon;

public class UserConverter implements Converter<String> {
    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        WriteCellData<Object> emptyData = new WriteCellData<>("");
        emptyData.setType(CellDataTypeEnum.STRING);
        if(StrUtil.isEmpty(value)) {
            return emptyData;
        }

        ISysUserService userService = Solon.context().getBean(ISysUserService.class);
        SysUser sysUser = userService.getMapper().selectOneByQuery(QueryWrapper.create().eq("user_name", value));
        if(ObjUtil.isNull(sysUser)) return new WriteCellData<>(value);
        return new WriteCellData<>(sysUser.getNickName());
    }
}